package daoimpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import pojo.Category;
import pojo.UserProfile;
import dao.CategoryDAO;

@Repository("CategoryDAO")
public class CategoryDAOImpl implements CategoryDAO {

	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public List<Category> getListCategory() {
		List<Category> ds = null;
		
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}
		catch(HibernateException ex)
		{
			session = sessionFactory.openSession();
		}
			
        try {
            String hql = "Select cate from Category cate";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
        	session.close();
        }
        return ds;
	}

	public Category getCategoryFromId(String id) {
		Category category = null;
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}
		catch(HibernateException ex)
		{
			session = sessionFactory.openSession();
		}
			
        try {
        	String hql = "Select cate from Category cate where cate.idcategory=:idcategory";
            Query query = session.createQuery(hql);
            query.setParameter("idcategory", id);
            category = (Category)query.uniqueResult();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
        	session.close();
        }
        return category;
	}

}

